package com.nbcio.modules.crm.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.customer.domain.CrmCustomer;
import com.nbcio.modules.crm.customer.service.ICrmCustomerService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM客户管理控制器
 */
@Tag(name = "CRM客户管理")
@RestController
@RequestMapping("/crm/customer")
@RequiredArgsConstructor
public class CrmCustomerController {

    private final ICrmCustomerService customerService;

    /**
     * 分页查询客户
     */
    @GetMapping("/page")
    public R<IPage<CrmCustomer>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String customerCode,
            @RequestParam(required = false) String customerName,
            @RequestParam(required = false) String customerType,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long levelId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String source,
            @RequestParam(required = false) Long ownerUserId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        LambdaQueryWrapper<CrmCustomer> qw = new LambdaQueryWrapper<>();
        if (customerCode != null && !customerCode.isEmpty()) {
            qw.like(CrmCustomer::getCustomerCode, customerCode);
        }
        if (customerName != null && !customerName.isEmpty()) {
            qw.like(CrmCustomer::getCustomerName, customerName);
        }
        if (customerType != null && !customerType.isEmpty()) {
            qw.eq(CrmCustomer::getCustomerType, customerType);
        }
        if (categoryId != null) {
            qw.eq(CrmCustomer::getCategoryId, categoryId);
        }
        if (levelId != null) {
            qw.eq(CrmCustomer::getLevelId, levelId);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmCustomer::getStatus, Integer.parseInt(status));
        }
        if (source != null && !source.isEmpty()) {
            qw.eq(CrmCustomer::getSource, source);
        }
        if (ownerUserId != null) {
            qw.eq(CrmCustomer::getOwnerUserId, ownerUserId);
        }
        if (startTime != null && endTime != null && !startTime.isEmpty() && !endTime.isEmpty()) {
            qw.between(CrmCustomer::getCreateTime, startTime, endTime);
        }
        qw.orderByDesc(CrmCustomer::getCreateTime);
        IPage<CrmCustomer> page = customerService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 根据ID查询客户
     */
    @GetMapping("/{id}")
    public R<CrmCustomer> get(@PathVariable("id") String id) {
        CrmCustomer customer = customerService.getById(Long.parseLong(id));
        return R.ok(customer);
    }

    /**
     * 新增客户
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmCustomer customer) {
        return R.ok(customerService.save(customer));
    }

    /**
     * 更新客户
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmCustomer customer) {
        customer.setCustomerId(Long.parseLong(id));
        return R.ok(customerService.updateById(customer));
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(customerService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除客户
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(customerService.removeByIds(longIds));
    }
}

